package com.nf.health.app.http;

import com.android.volley.AuthFailureError;
import com.android.volley.NetworkError;
import com.android.volley.NetworkResponse;
import com.android.volley.NoConnectionError;
import com.android.volley.ParseError;
import com.android.volley.ServerError;
import com.android.volley.TimeoutError;
import com.android.volley.VolleyError;

public class VolleyErrorHelper {

	public static String getMessage(Object error) {
		if( error instanceof ServerError || error instanceof AuthFailureError) {
			return handleServerError(error);
		} else if( error instanceof ParseError) {
			return "解析错误";
		} else if( error instanceof NoConnectionError || error instanceof NetworkError) {
			return "请检查网络连接！";
		} else if( error instanceof TimeoutError) {
			return "请求超时！";
		}
		return "请求错误！";
	}

	private static String handleServerError(Object err) {
		VolleyError error = (VolleyError) err;

		NetworkResponse response = error.networkResponse;

		if (response != null) {
			switch (response.statusCode) {
			case 404:
			case 422:
			case 401:
				String s = new String(response.data);
				s = s.trim();
				return "服务器错误";
			default:
				return "服务器错误！";
			}
		}
		return "请求错误！";
	}
}
